package com.effectiv.gooruda.rest.api.service;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.rest.api.exception.QueueOperationException;

@Component
public class QueueViewMBeanLocator {
	
	@Autowired
	private MBeanServerConnection mBeanServerConnection;
	
	public QueueViewMBean getQueueView(String queueName) {
		ObjectName queueON;
		QueueViewMBean queueMbean = null;
		try {
			queueON = new ObjectName(
					"org.apache.activemq:BrokerName=amq-broker,Type=Queue,Destination="
							+ queueName);
			queueMbean = (QueueViewMBean) MBeanServerInvocationHandler
					.newProxyInstance(mBeanServerConnection, queueON,
							QueueViewMBean.class, false);

		} catch (MalformedObjectNameException e) {
			throw new QueueOperationException(
					"Error finding queues for application", e);
		}
		return queueMbean;
	}
}
